package com.wanxi.shop.service.impl;

import com.wanxi.shop.api.entity.NearShopQueryEntity;
import com.wanxi.shop.api.entity.ShopEntity;
import com.wanxi.shop.api.feign.ShopFeignClient;
import com.wanxi.shop.api.query.NearShopQuery;
import com.wanxi.shop.api.result.CommonResult;
import com.wanxi.shop.api.vo.*;
import com.wanxi.shop.mapper.ShopMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


@RestController
public class ShopServiceApiImpl implements ShopFeignClient {
    @Resource
    private ShopMapper shopMapper;

    private static Logger log = LoggerFactory.getLogger(ShopServiceApiImpl.class);

    /**
     * @author jinfang
     * @description // 根据店铺id查询店铺名称
     * @date  2021/1/14
     * @params id
     * @return CommonResult
     **/
    @Override
    public CommonResult<String> findShopNameById(Long id) {
        String message = "";
        try {
            String shopName = shopMapper.findShopNameById(id);
            return CommonResult.success(shopName);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

    /**
     * @author jinfang
     * @description // 店家根据id查看自己的店铺信息
     * @date  2021/1/14
     * @params id
     * @return CommonResult
     **/
    @Override
    @GetMapping("shop/findShopById")
    public CommonResult<ShopFindByIdOutVo> findShopById(Long id) {
        String message = "";
        try {
            //去数据库根据id查找
            ShopFindByIdOutVo shopFindByIdOutVo = shopMapper.findShopById(id);
            return CommonResult.success(shopFindByIdOutVo);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

    /**
     * @author jinfang
     * @description // 店家根据id修改自己的店铺信息
     * @date  2021/1/14
     * @params id
     * @return CommonResult
     **/
    @Override
    @PutMapping("shop/updateShopById")
    public CommonResult updateShop(ShopUpdateInVo shopUpdateInVo) {
        String message = "";
        try {
            //封装成Entity
            ShopEntity shopEntity = new ShopEntity();
            BeanUtils.copyProperties(shopUpdateInVo,shopEntity);
            //去数据库修改店铺
            int deleteId = shopMapper.updateShop(shopEntity);
            return CommonResult.success(deleteId);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

    /**
     * @author jinfang
     * @description 用户查询自己几公里范围内的店铺
     * @date  2021/1/15
     * @param nearShopQuery
     * @return
     */
    @Override
    @GetMapping("user/findNearShop")
    public CommonResult<List<FindNearShopOutVO>> findNearShop(NearShopQuery nearShopQuery) {
        String message = "";
        try {
            //封装成Entity
            NearShopQueryEntity nearShopQueryEntity = new NearShopQueryEntity();
            BeanUtils.copyProperties(nearShopQuery,nearShopQueryEntity);
            //去数据库查询
            List<FindNearShopOutVO> list = shopMapper.findNearShop(nearShopQueryEntity);
            return CommonResult.success(list);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

    /**
     * @author jinfang
     * @description 用户根据店铺名字查询店铺
     * @date  2021/1/17
     * @param shopName
     * @return
     */
    @Override
    @GetMapping("user/findShopName")
    public CommonResult<List<FindShopNameOutVO>> findShopName(String shopName) {
        String message = "";
        try {
            //去数据库查询
            List<FindShopNameOutVO> list = shopMapper.findShopName(shopName);
            return CommonResult.success(list);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

    @Override
    @GetMapping("user/findSkuByType")
    public CommonResult<List<SkuFindAllOutVo>> findSkuByType(Long typeId) {
        String message = "";
        try {
            //去数据库查询
            List<SkuFindAllOutVo> list = shopMapper.findSkuByType(typeId);
            return CommonResult.success(list);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

    @Override
    @GetMapping("user/findSkuByShopId")
    public CommonResult<List<SkuFindByShopOutVo>> findSkuByShopId(Long shopId) {
        String message = "";
        try {
            //去数据库查询
            List<SkuFindByShopOutVo> list = shopMapper.findSkuByShopId(shopId);
            return CommonResult.success(list);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

    @Override
    @GetMapping("user/findAllShop")
    public CommonResult<List<FindShopAll>> findAllShop(Long shopId) {
        String message = "";
        try {
            //去数据库查询
            List<FindShopAll> list = shopMapper.findAllShop(shopId);
            return CommonResult.success(list);
        } catch (BeansException e) {
            message = e.getMessage();
            log.info(message);
        }
        return CommonResult.failed(message);
    }

}
